package com.tools.cleanmaster.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.manager.NotificationSceneManager;
import com.tools.cleanmaster.scenenew.NetflowScreenOffHelper;
import com.tools.cleanmaster.store.SceneSharedPrefManager;
import com.tools.cleanmaster.ui.notification.item.NetflowScreenOffItem;
import com.tools.cleanmaster.utils.FeatureConfig;
import com.tools.cleanmaster.utils.LogHelper;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 开屏，关屏检测场景点
 */
public class SceneScreenSwitchReceiver extends BroadcastReceiver {
    public static final String TAG = "Scene";

    private static SceneScreenSwitchReceiver mInstance;
    private AtomicBoolean mRegisterFlag = new AtomicBoolean(false);

    private SceneScreenSwitchReceiver() {
    }

    public static SceneScreenSwitchReceiver getInstance() {
        if (mInstance == null) {
            synchronized (SceneScreenSwitchReceiver.class) {
                if (mInstance == null) {
                    mInstance = new SceneScreenSwitchReceiver();
                    mInstance.init();
                }
            }
        }
        return mInstance;
    }

    private Context mContext;

    private void init() {
        mContext = CleanApplication.getInstance();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (Intent.ACTION_SCREEN_ON.equals(action)) {    // 开屏
            netflowOnScreen();
        } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {    // 关屏
            netflowOffScreen();
        }
    }

    public void start() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
        mContext.registerReceiver(this, intentFilter);
        mRegisterFlag.set(true);
    }

    public synchronized void stop() {
        if (mRegisterFlag.getAndSet(false)) {
            mContext.unregisterReceiver(this);
        }
    }


    /**
     * 锁屏记录网络流量
     */
    private void netflowOffScreen() {
        NetflowScreenOffHelper.getInstance().recordData();
    }

    /**
     * 开屏计算网络流量
     */
    private void netflowOnScreen() {
        if (SceneSharedPrefManager.getSceneRestartTime() != 0) {
            if (FeatureConfig.DEBUG_LOG) {
                LogHelper.d("Scene", "场景化检测已经停止，无需再安排锁屏流量场景点检查任务 ");
            }
            return;
        }
        if (FeatureConfig.DEBUG_LOG) {
            LogHelper.d(TAG, "场景化锁屏流量场景点，检查开始");
        }
        NotificationSceneManager sceneManager = NotificationSceneManager.getInstance();
        if (!sceneManager.checkAndRestTimeLimit()) {
            return;
        }
        String pkgName = NetflowScreenOffHelper.getInstance().calcData();
        if (!TextUtils.isEmpty(pkgName)) {
            NetflowScreenOffItem netflowItem = new NetflowScreenOffItem(pkgName);
            if (netflowItem.checkRules()) {
                NotificationSceneManager.getInstance().setNetflowShowPkg(pkgName);
                NotificationSceneManager.getInstance().showSceneNotification(netflowItem);
            }
        }
    }
}
